package cn.appsys.service.developer;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.appsys.dao.appversion.AppVersionMapper;
import cn.appsys.pojo.AppVersion;

@Service
public class AppVersionServiceImpl implements AppVersionService {

	@Resource
	private AppVersionMapper appVersionMapper;

	@Override
	public List<AppVersion> getAppVersionList(String id) {
		List<AppVersion> list = appVersionMapper.getAppVersionList(id);
		List<AppVersion> appVersionList = new ArrayList<AppVersion>();
		if (id == null) {
			id = "0";
		}
		for (AppVersion appVersion : list) {
			String path = appVersion.getDownloadLink();
			if (path!=null) {
				appVersion.setDownloadLink(path.substring(0, path.lastIndexOf("/")) + "/" + "appId/"
						+ appVersion.getAppId() + "/vid/" + appVersion.getId());
				appVersionList.add(appVersion);
			}
		}
		
		return appVersionList;
	}

	@Override
	public boolean addAppVersion(AppVersion appVersion) {
		boolean flag = false;
		if (appVersionMapper.addAppVersion(appVersion) > 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public AppVersion getAppVersion(String appId,String id) {
		AppVersion appVersion = appVersionMapper.getAppVersion(appId,id);
		String path = appVersion.getDownloadLink();
		if (path!=null) {
			appVersion.setDownloadLink(
					path.substring(0,path.lastIndexOf("/"))+"/"+"appId/"+appId+"/vid/"+appVersion.getId());
		}
				return appVersion;
	}

	@Override
	public boolean delAppVersion(String id) {
		boolean flag = false;
		if (appVersionMapper.delAppVersion(id) > 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public boolean delFile(String id) {
		boolean flag = false;
		if (appVersionMapper.delFile(id) > 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public boolean modifyAppVersion(AppVersion appVersion) {
		boolean flag = false;
		if (appVersionMapper.modifyAppVersion(appVersion) > 0) {
			flag = true;
		}
		return flag;
	}

}
